/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim.helpers.statistics;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sim.misc.DateUtils;
import sim.helpers.Helper;
import sim.helpers.Variables;

/**
 *
 * @author artem
 */
public class StatisticsExporter {

    public StatisticsExporter() {
    }

    public static boolean Export(Helper helper) {
        FileOutputStream fos = null;
        boolean ret = true;
        String filename = System.getProperty("user.home") + "/Desktop/"
                + Variables.changingConstant.toUpperCase() +
                "V" + Variables.minValue + Variables.maxValue + "I" + Variables.increment + "R" + Variables.repetitions + "D" + DateUtils.now(DateUtils.DATE_FORMAT_NOW_FULL);
        try {
            File of = new File(filename + ".msr");
            fos = new FileOutputStream(of);
            exportVariables(fos, helper);
            exportNLi(fos);
            exportCc(fos);
            exportAcc(fos);
        } catch (IOException ex) {
            Logger.getLogger(StatisticsExporter.class.getName()).log(Level.SEVERE, null, ex);
            ret = false;
        } finally {
            try {
                fos.close();
                return ret;
            } catch (final IOException ex) {
                ret = false;
                Logger.getLogger(StatisticsExporter.class.getName()).log(Level.SEVERE, null, ex);
                return ret;
            }
        }
    }

    private static void exportNLi(FileOutputStream fos) throws IOException {
        fos.write("NLI:\n".getBytes());
        for (ValueHolder vh : Variables.NLiSH.getValueList()) {
            fos.write((String.valueOf(vh.getIndex()) + "\t" + String.valueOf(vh.getValue())+ "\n").getBytes());
        }
    }

    private static void exportCc(FileOutputStream fos) throws IOException {
        fos.write("\nCC:\n".getBytes());
        for (ValueHolder vh : Variables.CcSH.getValueList()) {
            fos.write((String.valueOf(vh.getIndex()) + "\t" + String.valueOf(vh.getValue()) + "\n").getBytes());
        }
    }

    private static void exportAcc(FileOutputStream fos) throws IOException {
        fos.write("\nACC:\n".getBytes());
        for (ValueHolder vh : Variables.AccSH.getValueList()) {
            fos.write((String.valueOf(vh.getIndex()) + "\t" + String.valueOf(vh.getValue()) + "\n").getBytes());
        }
    }
    
    private static void exportVariables(FileOutputStream fos, Helper helper) throws IOException {
        fos.write(("SP:\n").getBytes());
        if (Variables.changingConstant.trim().equalsIgnoreCase("qos")) {
            fos.write(("N\t" + String.valueOf(helper.N) + "\n").getBytes());
            fos.write(("H\t" + String.valueOf(helper.H) + "\n").getBytes());
            fos.write(("L\t" + String.valueOf(helper.L) + "\n").getBytes());
            fos.write(("QOS\t" + String.valueOf(Variables.minValue) + " .. " + String.valueOf(Variables.maxValue) + "\n").getBytes());
            fos.write(("SD\t" + String.valueOf(helper.SD) + "\n").getBytes());
        } else if (Variables.changingConstant.trim().equalsIgnoreCase("n")) {
            fos.write(("N\t" + String.valueOf(Variables.minValue) + " .. " + String.valueOf(Variables.maxValue) + "\n").getBytes());
            fos.write(("H\t" + String.valueOf(helper.H) + "\n").getBytes());
            fos.write(("L\t" + String.valueOf(helper.L) + "\n").getBytes());
            fos.write(("QOS\t" + String.valueOf(helper.QOS) + "\n").getBytes());
            fos.write(("SD\t" + String.valueOf(helper.SD) + "\n").getBytes());
        } else if (Variables.changingConstant.trim().equalsIgnoreCase("h")) {
            fos.write(("N\t" + String.valueOf(helper.N) + "\n").getBytes());
            fos.write(("H\t" + String.valueOf(Variables.minValue) + " .. " + String.valueOf(Variables.maxValue) + "\n").getBytes());
            fos.write(("L\t" + String.valueOf(helper.L) + "\n").getBytes());
            fos.write(("QOS\t" + String.valueOf(helper.QOS) + "\n").getBytes());
            fos.write(("SD\t" + String.valueOf(helper.SD) + "\n").getBytes());
        } else if (Variables.changingConstant.trim().equalsIgnoreCase("l")) {
            fos.write(("N\t" + String.valueOf(helper.N) + "\n").getBytes());
            fos.write(("H\t" + String.valueOf(helper.H) + "\n").getBytes());
            fos.write(("L\t" + String.valueOf(Variables.minValue) + " .. " + String.valueOf(Variables.maxValue) + "\n").getBytes());
            fos.write(("QOS\t" + String.valueOf(helper.QOS) + "\n").getBytes());
            fos.write(("SD\t" + String.valueOf(helper.SD) + "\n").getBytes());
        } else if (Variables.changingConstant.trim().equalsIgnoreCase("sd")) {
            fos.write(("N\t" + String.valueOf(helper.N) + "\n").getBytes());
            fos.write(("H\t" + String.valueOf(helper.H) + "\n").getBytes());
            fos.write(("L\t" + String.valueOf(helper.L) + "\n").getBytes());
            fos.write(("QOS\t" + String.valueOf(helper.QOS) + "\n").getBytes());
            fos.write(("SD\t"+ String.valueOf(Variables.minValue) + " .. " + String.valueOf(Variables.maxValue) + "\n").getBytes());
        } else {
            fos.write("Did not find a valid changing constant to iterate, switching to QOS".getBytes());
            fos.write(("N\t" + String.valueOf(helper.N) + "\n").getBytes());
            fos.write(("H\t" + String.valueOf(helper.H) + "\n").getBytes());
            fos.write(("L\t" + String.valueOf(helper.L) + "\n").getBytes());
            fos.write(("QOS\t" + String.valueOf(Variables.minValue) + " .. " + String.valueOf(Variables.maxValue) + "\n").getBytes());
            fos.write(("SD\t" + String.valueOf(helper.SD) + "\n").getBytes());
        }
        fos.write(("R\t" + String.valueOf(helper.R) + "\n").getBytes());
        fos.write(("Rep\t" + String.valueOf(Variables.repetitions) + "\n\n").getBytes());
    }
}
